/*
moxl - Modular XML Library
Copyright (c) 2007, Malcolm Sparks

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package moxl.xpath.parser.states.primaryexpr;

import moxl.tokenizer.Token;
import moxl.tokenizer.TokenStream;
import moxl.xpath.parser.XPathParser;
import moxl.xpath.parser.XPathParserException;
import moxl.xpath.parser.states.XPathParserState;
import moxl.xpath.parser.types.Expression;
import moxl.xpath.parser.types.FunctionCall;
import moxl.xpath.parser.types.NonTerminal;
import moxl.xpath.parser.types.PathExpression;
import moxl.xpath.parser.types.VariableReference;
import moxl.xpath.parser.types.XPathLiteralOperand;
import moxl.xpath.parser.types.XPathNumberOperand;
import moxl.xpath.tokenizer.XPathTokenTypes;

/**
 * See XPath production [15]
 */
public class XPathPrimaryExpressionStartState extends XPathParserState
{

	private static XPathParserState instance = new XPathPrimaryExpressionStartState();

	public static XPathParserState getInstance()
	{
		return instance;
	}

	@Override
    public void acceptVariableReference(XPathParser parser, TokenStream tstream, Token token, NonTerminal nonTerminal) throws XPathParserException
	{
		((Expression) nonTerminal).addOperand(new VariableReference(token.getImage()));
		parser.changeState(XPathPrimaryExpressionEndState.getInstance());
	}

	@Override
    public void acceptOpenParenthesis(XPathParser parser, TokenStream tstream, Token token, NonTerminal nonTerminal) throws XPathParserException
	{
		Expression subexpr = parser.begetParser().parseExpressionFromStream(tstream);
		((Expression) nonTerminal).addOperand(subexpr);
		Token closeParensLookahead = lookahead(tstream, 1);
		if (closeParensLookahead.getType().equals(XPathTokenTypes.CLOSE_PARENTHESIS))
		{
			read(tstream);
			parser.changeState(XPathPrimaryExpressionEndState.getInstance());
		}
		else
		{
			throw new XPathParserException("Unbalanced parenthesis at " + token.getLocation() + ", token seen is " + closeParensLookahead);
		}
	}

	@Override
    public void acceptLiteral(XPathParser parser, TokenStream tstream, Token token, NonTerminal nonTerminal) throws XPathParserException
	{
		((Expression) nonTerminal).addOperand(new XPathLiteralOperand(token.getImage()));
		parser.changeState(XPathPrimaryExpressionEndState.getInstance());
	}

	@Override
    public void acceptNumber(XPathParser parser, TokenStream tstream, Token token, NonTerminal nonTerminal) throws XPathParserException
	{
		((Expression) nonTerminal).addOperand(new XPathNumberOperand(token.getImage()));
		parser.changeState(XPathPrimaryExpressionEndState.getInstance());
	}

	@Override
    public void acceptFunctionName(XPathParser parser, TokenStream tstream, Token token, NonTerminal nonTerminal) throws XPathParserException
	{
		tstream.pushback(token);
		FunctionCall fnCall = parser.begetParser().parseFunctionCallFromStream(tstream);
		((Expression) nonTerminal).addOperand(fnCall);
		parser.changeState(XPathPrimaryExpressionEndState.getInstance());
	}

	@Override
    public void acceptAtSymbol(XPathParser parser, TokenStream tstream, Token token, NonTerminal nonTerminal) throws XPathParserException
	{
		tstream.pushback(token);
		readLocationPath(tstream, nonTerminal, parser);
	}

	@Override
    public void acceptAxisName(XPathParser parser, TokenStream tstream, Token token, NonTerminal nonTerminal) throws XPathParserException
	{
		tstream.pushback(token);
		readLocationPath(tstream, nonTerminal, parser);
	}
	
	@Override
    public void acceptNameTest(XPathParser parser, TokenStream tstream, Token token, NonTerminal nonTerminal) throws XPathParserException
    {
		tstream.pushback(token);
		readLocationPath(tstream, nonTerminal, parser);
    }
	
	@Override
    public void acceptNodeType(XPathParser parser, TokenStream tstream, Token token, NonTerminal nonTerminal) throws XPathParserException
    {
		tstream.pushback(token);
		readLocationPath(tstream, nonTerminal, parser);
    }

	@Override
    public void acceptOperator(XPathParser parser, TokenStream tstream, Token token, NonTerminal nonTerminal) throws XPathParserException
	{
		if (token.getImage().equals("/"))
		{
			tstream.pushback(token);
			readLocationPath(tstream, nonTerminal, parser);
		}
		else
		{
			super.acceptOperator(parser, tstream, token, nonTerminal);
		}
	}

	private void readLocationPath(TokenStream tstream, NonTerminal nonTerminal, XPathParser parser) throws XPathParserException
	{
		PathExpression locationPath = parser.begetParser().parseLocationPathFromStream(tstream);
		((Expression) nonTerminal).addOperand(locationPath);
		parser.changeState(XPathPrimaryExpressionEndState.getInstance());
	}

}
